package vn.aptech.leaveManagementSystem.view.admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import vn.aptech.leaveManagementSystem.dao.DBManager;

public class ViewUser extends JFrame{
	DBManager con = new DBManager();
	Connection conn;
	String sql;
	ResultSet rs;
	
	
	JPanel pnlMain;
	private JTextField txtFirstname;
	private JTextField txtLastname;
	private JTextField txtPassword;
	private JTextField txtPhone;
	private JTextField txtEmail;
	private JTextArea txtAddress;
	private JTextField txtBirthday;
	private JTextField txtJoindate;

	public ViewUser() {

		pnlMain = new JPanel();
		pnlMain.setLayout(null);
		
		
		
		getContentPane().add(pnlMain);
		
		JLabel lblNewLabel = new JLabel("ID :");
		lblNewLabel.setBounds(10, 30, 27, 14);
		pnlMain.add(lblNewLabel);
		
		JLabel lblFirst = new JLabel("First name :");
		lblFirst.setBounds(10, 55, 69, 14);
		pnlMain.add(lblFirst);
		
		JLabel lblLastName = new JLabel("Last name :");
		lblLastName.setBounds(10, 80, 69, 14);
		pnlMain.add(lblLastName);
		
		JLabel lblPassWord = new JLabel("Password :");
		lblPassWord.setBounds(10, 342, 69, 14);
		pnlMain.add(lblPassWord);
		
		JLabel lblPhone = new JLabel("Phone :");
		lblPhone.setBounds(10, 244, 69, 14);
		pnlMain.add(lblPhone);
		
		JLabel lblAddress = new JLabel("Address :");
		lblAddress.setBounds(10, 155, 69, 14);
		pnlMain.add(lblAddress);
		
		JLabel lblEmail = new JLabel("Email :");
		lblEmail.setBounds(10, 219, 69, 14);
		pnlMain.add(lblEmail);
		
		JLabel lblPosition = new JLabel("Position :");
		lblPosition.setBounds(10, 292, 69, 14);
		pnlMain.add(lblPosition);
		
		JLabel lblDepartment = new JLabel("Department :");
		lblDepartment.setBounds(10, 268, 69, 14);
		pnlMain.add(lblDepartment);
		
		String strGen[] = {"Female","Male"};
		final JComboBox cbbGender = new JComboBox(strGen);
		cbbGender.setBounds(89, 102, 181, 20);
		pnlMain.add(cbbGender);
		
		String strPos[] = {"Admin","Director","Manager","Employee"};
		final JComboBox cbbPosition = new JComboBox(strPos);
		cbbPosition.setBounds(89, 289, 181, 20);
		pnlMain.add(cbbPosition);
		
		String strDepartment[] = {"Business Department","Technical Department","HR Department"};
		final JComboBox cbbDepartment = new JComboBox(strDepartment);
		cbbDepartment.setBounds(89, 265, 181, 20);
		pnlMain.add(cbbDepartment);
		
		final JComboBox cbbID = new JComboBox();
		cbbID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sql = "select * from [Employee] where IDEmp = ?";
				try {
					conn = con.getConn();
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, cbbID.getSelectedItem().toString());
					ResultSet rs = ps.executeQuery();
					
					while(rs.next())
		            {
		                txtFirstname.setText(rs.getString(2).trim());
		                txtLastname.setText(rs.getString(3).trim());
		                cbbGender.setSelectedIndex(rs.getInt(4));
		                txtBirthday.setText(rs.getDate(5).toString());
		                txtAddress.setText(rs.getString(6).trim());
		                txtEmail.setText(rs.getString(7));
		                txtPhone.setText(rs.getString(8).trim());
		                cbbDepartment.setSelectedIndex(rs.getInt(9)-1);
		                cbbPosition.setSelectedIndex(rs.getInt(10)-1);
		                txtJoindate.setText(rs.getDate(11).toString());
		                txtPassword.setText(rs.getString(13).trim());
		                /*this.cmbPosition.setSelectedIndex(rs.getInt(3)-1);
		                this.cmbDepartment.setSelectedIndex(rs.getInt(4)-1);
		                this.cmbSeniority.setSelectedIndex(rs.getInt(5)-1);
		                this.txtPhone.setText(rs.getString(7).trim());
		                this.txtAddress.setText(rs.getString(8).trim());
		                this.txtEmail.setText(rs.getString(9).trim());
		                this.txtPass.setText(rs.getString(11).trim());*/                
		                
		                
		                
		            }
		            rs.close();
		            conn.close();
					//ps.setString(1, )
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		cbbID.setBounds(94, 27, 176, 20);
		pnlMain.add(cbbID);
		
		txtFirstname = new JTextField();
		txtFirstname.setBounds(90, 52, 274, 20);
		pnlMain.add(txtFirstname);
		txtFirstname.setColumns(10);
		
		txtLastname = new JTextField();
		txtLastname.setBounds(89, 77, 275, 20);
		pnlMain.add(txtLastname);
		txtLastname.setColumns(10);
		
		txtPassword = new JTextField();
		txtPassword.setBounds(89, 339, 275, 20);
		pnlMain.add(txtPassword);
		txtPassword.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setBounds(88, 241, 276, 20);
		pnlMain.add(txtPhone);
		txtPhone.setColumns(10);
		
		txtAddress = new JTextArea();
		
		JScrollPane scrollpane = new JScrollPane(txtAddress);
		scrollpane.setBounds(89, 150, 275, 62);
		pnlMain.add(scrollpane);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(89, 216, 275, 20);
		pnlMain.add(txtEmail);
		txtEmail.setColumns(10);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				AdminForm adminForm =  new AdminForm();
				adminForm.setVisible(true);
			}
		});
		btnCancel.setBounds(168, 375, 89, 23);
		pnlMain.add(btnCancel);
		
		JLabel lblGender = new JLabel("Gender :");
		lblGender.setBounds(10, 105, 46, 14);
		pnlMain.add(lblGender);
		
		
		
		JLabel lblBirthday = new JLabel("Birthday :");
		lblBirthday.setBounds(10, 130, 69, 14);
		pnlMain.add(lblBirthday);
		
		txtBirthday = new JTextField();
		txtBirthday.setBounds(89, 127, 275, 20);
		pnlMain.add(txtBirthday);
		txtBirthday.setColumns(10);
		
		JLabel lblJoinDate = new JLabel("Join date :");
		lblJoinDate.setBounds(10, 317, 69, 14);
		pnlMain.add(lblJoinDate);
		
		txtJoindate = new JTextField();
		txtJoindate.setBounds(89, 314, 275, 20);
		pnlMain.add(txtJoindate);
		txtJoindate.setColumns(10);
		
		
		
		
		
		
		//get Employee details
		sql = "select * from Employee";
		Statement st;
		try {
			conn = con.getConn();
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				cbbID.addItem(rs.getString(1));
			}
			rs.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		setVisible(true);
		setTitle("Information User");
		setSize(390, 569);
		setLocationRelativeTo(null);
	
	}

}
